package api

import (
	"github.com/LyricTian/gin-admin/v7/internal/app/bll"
	"github.com/LyricTian/gin-admin/v7/internal/app/ginx"
	"github.com/LyricTian/gin-admin/v7/internal/app/schema"
	"github.com/gin-gonic/gin"
	"github.com/google/wire"
)

// SetSet 注入Set 系统设置
var SetSet = wire.NewSet(wire.Struct(new(Set), "*"))

// Set 系统设置
type Set struct {
	SetBll *bll.Set
}

// Query 查询数据
func (a *Set) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.SetQueryParam
	if err := ginx.ParseQuery(c, &params); err != nil {
		ginx.ResError(c, err)
		return
	}

	params.Pagination = true
	result, err := a.SetBll.Query(ctx, params)
	if err != nil {
		ginx.ResError(c, err)
		return
	}

	ginx.ResPage(c, result.Data, result.PageResult)
}

// Get 查询指定数据
func (a *Set) Get(c *gin.Context) {
	ctx := c.Request.Context()
	item, err := a.SetBll.Get(ctx, c.Param("id"))
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResSuccess(c, item)
}

// Create 创建数据
func (a *Set) Create(c *gin.Context) {
	ctx := c.Request.Context()
	var item schema.Set
	if err := ginx.ParseJSON(c, &item); err != nil {
		ginx.ResError(c, err)
		return
	}
	item.Creator = ginx.GetUserID(c)
	result, err := a.SetBll.Create(ctx, item)
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResSuccess(c, result)
}

// Update 更新数据
func (a *Set) Update(c *gin.Context) {
	ctx := c.Request.Context()
	var item schema.SetUpdate
	if err := ginx.ParseJSON(c, &item); err != nil {
		ginx.ResError(c, err)
		return
	}

	err := a.SetBll.Update(ctx, c.Param("id"), *item.ToSet())
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResOK(c)
}

// Delete 删除数据
func (a *Set) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.SetBll.Delete(ctx, c.Param("id"))
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResOK(c)
}

// Enable 启用数据
func (a *Set) Enable(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.SetBll.UpdateStatus(ctx, c.Param("id"), 1)
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResOK(c)
}

// Disable 禁用数据
func (a *Set) Disable(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.SetBll.UpdateStatus(ctx, c.Param("id"), 2)
	if err != nil {
		ginx.ResError(c, err)
		return
	}
	ginx.ResOK(c)
}
